package com.ikdong.toodle.activity;

import com.ikdong.toodle.R;
import com.ikdong.toodle.util.AccountManager;
import com.ikdong.toodle.util.AuthManager;
import com.ikdong.toodle.util.Constant;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

public class LoginActivity extends Activity {
	private String PARAM = "";
	private AccountManager am;
	private AuthManager aum;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.login);
		am = new AccountManager(this);
		aum = new AuthManager(this);
		initView();
	}

	private void initView() {		
		TextView.OnEditorActionListener textListener = new TextView.OnEditorActionListener() {			
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				new UserLoginTask().execute();
				return true;
			}
		};
		EditText email = (EditText) this.findViewById(R.id.login_email);
		EditText password = (EditText) this.findViewById(R.id.login_password);
		email.setOnEditorActionListener(textListener);
		password.setOnEditorActionListener(textListener);

		this.findViewById(R.id.login_submit).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {					
						new UserLoginTask().execute();
					}
				});
	}

	public class UserLoginTask extends AsyncTask<Void, Void, Boolean>{
		@Override
		protected Boolean doInBackground(Void... params) {
			Boolean flag = false;
			try{
				handleProgress.sendEmptyMessage(0);
				String email = ((EditText) findViewById(R.id.login_email)).getText().toString();
				String password = ((EditText) findViewById(R.id.login_password)).getText().toString();
				if(email.trim().length()>0 && password.trim().length()>0){
					flag = aum.auth(email,password);
					if(flag){
						am.update(aum.getKey());
					}
				}
			}catch(Exception e){
				Log.e(Constant.TAG, "UserLoginTask", e);
			}
			return flag;
		}
		
		@Override
        protected void onPostExecute(final Boolean isLogin) {
			Message msg = new Message();
			Bundle bundle = new Bundle();
			bundle.putBoolean(PARAM, isLogin);
			msg.setData(bundle);
			handleLogin.sendMessage(msg);
		}
		
	}
	
	private Handler handleLogin = new Handler() {
        @Override
        public void handleMessage(Message msg) {    
        	boolean isLogin = msg.getData().getBoolean(PARAM);
        	if(isLogin){
        		Intent intent = new Intent(LoginActivity.this,TaskListActivity.class);
        		intent.putExtra(Constant.PARAM_LAUNCH, true);
        		LoginActivity.this.startActivity(intent);
        	}else{
            	findViewById(R.id.login_progress).setVisibility(View.GONE);
    			findViewById(R.id.login_form).setVisibility(View.VISIBLE);
    			findViewById(R.id.login_error_message).setVisibility(View.VISIBLE);
        	}
        }
    };
    
    private Handler handleProgress = new Handler() {
        @Override
        public void handleMessage(Message msg) {    
			findViewById(R.id.login_progress).setVisibility(View.VISIBLE);
			findViewById(R.id.login_form).setVisibility(View.GONE);
        }
    };
}
